package com.ejuornal.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ejuornal.bean.Page;
import com.ejuornal.bean.Periodical;
import com.ejuornal.bean.vo.VO_SubScribe;
import com.ejuornal.dao.PeriodicalDao;
import com.ejuornal.service.PeriodicalService;

@Component("periodicalService")
public class PeriodicalServiceImpl implements PeriodicalService {
	private PeriodicalDao periodicalDao;

	public void add(Periodical periodical) {
		String sql = "INSERT INTO t_periodical VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		periodicalDao.add( sql,
				new Object[] { 
							null, 
							periodical.getCategoryId(),
							periodical.getUserId(),
							periodical.getName(),
							periodical.getCycle(),
							periodical.getCreateDate(),
							periodical.getPubDate(),
							periodical.getIntroduce(),
							periodical.getHostUnit(),
							periodical.getCover(),
							periodical.getTag(),
							periodical.getStatus() });
	}

	public void updateStatus(String status, int periodicalId) {
		String sql = "UPDATE t_periodical SET status = ? WHERE periodicalId = ?";
		periodicalDao.update(sql, new Object[]{ status, periodicalId });
	}
	
	public void updatePubDate(String pubDate, int periodicalId) {
		String sql = "UPDATE t_periodical SET pubDate = ? WHERE periodicalId = ?";
		periodicalDao.update(sql, new Object[]{ pubDate, periodicalId });
	}

	public Periodical findById(int periodicalId) {
		return periodicalDao.findById(periodicalId);
	}

	public Page<Periodical> findByStatus(int current_record, String url,
			String status) { 
		String hsql = "WHERE status = ?";
		return periodicalDao.find(current_record, url, hsql, new Object[]{ status });
	}

	public Page<Periodical> search(int current_record, String url,String keywords) {
		String hsql = "WHERE name LIKE ?";
		return periodicalDao.find(current_record, url, hsql, new Object[]{ "%"+keywords+"%" });
	}
	
	public List<Periodical> search(String keywords) {
	    return periodicalDao.findByName(keywords);
	}
	
	@Override
	public Page<Periodical> findByHostUnit(int current_record, String url,
			String hostUnit, String status) {
		String hoString = "%"+hostUnit+"%";	//注意这里没有''的
		//String hoString = "'%" + hostUnit + "%'"; 这是错误的写法
		String hsql = "WHERE hostUnit like ? AND status=?";
		return periodicalDao.find(current_record, url, hsql, new Object[]{ hoString, status });
	}

	public Page<Periodical> findAll(int current_record, String url) {
		return periodicalDao.find(current_record, url);
	}
	
	public List<Periodical> findTop(int count) {
	    return periodicalDao.findTop(count);
	}

	@Override
	public List<Periodical> findByCategoryId(int id, String status) {
	    return periodicalDao.findByCategoryId(id, status);
	}

	@Override
	public Page<Periodical> findByCategory(int current_record, String url, int categoryId, String status) {
		String hsql = "WHERE categoryId=? AND status=?";
		
		return periodicalDao.find(current_record, url, hsql, new Object[]{ categoryId, status });
	}

	@Override
	public List<Periodical> findByEditorChiefId(int id) {
		return periodicalDao.findByEditorChiefId(id);
	}

	@Override
	public Page<Periodical> findByUserIdPStatus(int current_record, String url, int userId, String status) {
		String sql_1 = "SELECT COUNT(*) FROM t_periodical p INNER JOIN t_category c ON p.categoryId = c.categoryId WHERE p.userId=? AND p.status=?";
		String sql_2 = "SELECT p.*,c.name AS cname FROM t_periodical p INNER JOIN t_category c ON p.categoryId = c.categoryId WHERE p.userId=? AND p.status=? ORDER BY periodicalId DESC LIMIT " + current_record + ", 10";
		
		return periodicalDao.find(current_record, url, sql_1, sql_2, new Object[]{ userId, status });
	}
	
	@Override
	public Page<Periodical> findByUserId(int current_record, String url, int userId) {
		String sql_1 = "SELECT COUNT(*) FROM t_periodical p INNER JOIN t_category c ON p.categoryId = c.categoryId WHERE p.userId=? AND p.status in (2,3)";
		String sql_2 = "SELECT p.*,c.name AS cname FROM t_periodical p INNER JOIN t_category c ON p.categoryId = c.categoryId WHERE p.userId=? AND p.status in (2,3) ORDER BY periodicalId DESC LIMIT " + current_record + ", 10";
		
		return periodicalDao.find(current_record, url, sql_1, sql_2, new Object[]{ userId });
	}
	
	@Override
	public Periodical findPeriodicalByPerId(int perId) {
		return periodicalDao.findPeriodicalByPerId(perId);
	}

	@Override
	public List<VO_SubScribe> find() {
		return periodicalDao.find();
	}

	@Override
	public List<Periodical> findByEditorId(int editorId, String status, int categoryId) {
		String sql = "SELECT p.* FROM t_editor AS e INNER JOIN t_periodical AS p ON e.periodicalId = p.periodicalId WHERE e.userId=? AND p.status=?";
		if(categoryId != 0) {
			sql = "SELECT p.* FROM t_editor AS e INNER JOIN t_periodical AS p ON e.periodicalId = p.periodicalId WHERE e.userId=? AND p.status=? AND p.categoryId=?";
			return periodicalDao.find(sql, new Object[]{ editorId, status, categoryId });
		}
		
		return periodicalDao.find(sql, new Object[]{ editorId, status });
	}

	public PeriodicalDao getPeriodicalDao() {
		return periodicalDao;
	}

	@Resource(name = "periodicalDao")
	public void setPeriodicalDao(PeriodicalDao periodicalDao) {
		this.periodicalDao = periodicalDao;
	}

	@Override
	public Periodical findPeriodicalByArticalId(int articalId) {
		String sql = "SELECT DISTINCT p.* FROM  t_artical AS a INNER JOIN t_per_periodical AS pp " +
				"ON a.perPeriodicalId = pp.perPeriodicalId INNER JOIN t_periodical AS p " +
				"ON pp.periodicalId = p.periodicalId WHERE a.articalId = ? ";
		return periodicalDao.findPeriodical(articalId, sql);
	}
	
	@Override
	public List<Periodical> periodicalAll(String status) {
		String sql = "select * from t_periodical where status=?";
		
		return periodicalDao.find(sql, new Object[]{ status });
	}
}
